﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using CoreEx.Common.Ioc;
using LinFu.IoC.Configuration;

namespace CoreEx.Common.Implementation.Ioc
{
    /// <summary>
    /// A class that is capable of finding a <see cref="Type"/> based on the type name.
    /// </summary>
    /// <remarks>
    /// If the type is not found in the currently executing assembly or in Mscorlib.dll, all currently loaded 
    /// assemblies will be searched.
    /// Regardless of the search method, the result is cached for later use.    
    /// </remarks>
    [Implements(typeof(ITypeFinder))]
    public class TypeFinder : ITypeFinder
    {
        public Type Find(string typeName)
        {            
            Type type = Type.GetType(typeName);
            if (type == null)
                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    type = assembly.GetType(typeName, false, true);
                    if (type != null)
                        break;
                }
            return type;
        }
    }
}
